package cl.clientews.rest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import cl.clienterest.conexion.ConstantesConexion;
import cl.clienterest.util.Util;

public class PublicacionesSOAWS {

	/**
	 * 
	 * @param pub_mon
	 * @param pub_des
	 * @param pers_rut
	 * @return
	 */
	public String addPublicacion(String pub_mon, String pub_des, String pers_rut) {
		DefaultHttpClient httpclient = new DefaultHttpClient();
		try {
			HttpPost httpPost = new HttpPost(ConstantesConexion.rest_url_base + "/publicacion/add");

			System.out.println(httpPost.getURI());

			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("pub_mon", pub_mon));
			nameValuePairs.add(new BasicNameValuePair("pub_des", pub_des));
			nameValuePairs.add(new BasicNameValuePair("pers_rut", pers_rut));
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

			HttpResponse responseRequest = httpclient.execute(httpPost);
			int rc = responseRequest.getStatusLine().getStatusCode();
			if ((rc != 204) && (rc != 201) && (rc != 200)) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ responseRequest.getStatusLine().getStatusCode());
			}

			String resp = Util.getStringFromInputStream(responseRequest.getEntity().getContent());

			return resp;
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			httpclient.getConnectionManager().shutdown();
		}
		return null;

	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	public String deletePublicacion(String id) {

		DefaultHttpClient httpclient = new DefaultHttpClient();
		try {
			HttpDelete httpDelete = new HttpDelete(ConstantesConexion.rest_url_base + "/publicacion/delete/" + id);

			System.out.println(httpDelete.getURI());

			HttpResponse responseRequest = httpclient.execute(httpDelete);
			int rc = responseRequest.getStatusLine().getStatusCode();
			if ((rc != 204) && (rc != 201) && (rc != 200)) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ responseRequest.getStatusLine().getStatusCode());
			}

			String resp = Util.getStringFromInputStream(responseRequest.getEntity().getContent());

			return resp;
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			httpclient.getConnectionManager().shutdown();
		}
		return null;

	}

	/**
	 * 
	 * @param id, numero
	 * @param campo, 0 cambia monto, 1 cambia descripcion
	 * @param new_valor, nuevo valor
	 * @return
	 */
	public String updatePublicacion(String id, int campo, String new_valor) {
		DefaultHttpClient httpclient = new DefaultHttpClient();
		try {
			HttpPut httpPost = new HttpPut(ConstantesConexion.rest_url_base + "/publicacion/update");

			System.out.println(httpPost.getURI());

			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("id", id));
			nameValuePairs.add(new BasicNameValuePair("campo", campo + ""));
			nameValuePairs.add(new BasicNameValuePair("nuevo_valor", new_valor));
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

			HttpResponse responseRequest = httpclient.execute(httpPost);
			int rc = responseRequest.getStatusLine().getStatusCode();
			if ((rc != 204) && (rc != 201) && (rc != 200)) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ responseRequest.getStatusLine().getStatusCode());
			}

			String resp = Util.getStringFromInputStream(responseRequest.getEntity().getContent());

			return resp;
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			httpclient.getConnectionManager().shutdown();
		}
		return null;

	}

	/**
	 * 
	 * @param buscar
	 * @return
	 */
	public String findPublicacion(String buscar) {

		DefaultHttpClient httpclient = new DefaultHttpClient();
		try {
			HttpGet httpGet = new HttpGet(ConstantesConexion.rest_url_base + "/publicacion/find/" + buscar);
			System.out.println(httpGet.getURI());

			HttpResponse responseRequest = httpclient.execute(httpGet);
			int rc = responseRequest.getStatusLine().getStatusCode();
			if ((rc != 204) && (rc != 201) && (rc != 200)) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ responseRequest.getStatusLine().getStatusCode());
			}

			String resp = Util.getStringFromInputStream(responseRequest.getEntity().getContent());

			return resp;
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			httpclient.getConnectionManager().shutdown();
		}
		return null;

	}

	/**
	 * 
	 * @param buscar
	 * @return
	 */
	public String findAllPublicacion(String buscar) {
		DefaultHttpClient httpclient = new DefaultHttpClient();
		try {
			HttpGet httpGet = new HttpGet(ConstantesConexion.rest_url_base + "/publicacion/findall/" + buscar);
			System.out.println(httpGet.getURI());

			HttpResponse responseRequest = httpclient.execute(httpGet);
			int rc = responseRequest.getStatusLine().getStatusCode();
			if ((rc != 204) && (rc != 201) && (rc != 200)) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ responseRequest.getStatusLine().getStatusCode());
			}

			String resp = Util.getStringFromInputStream(responseRequest.getEntity().getContent());

			return resp;
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			httpclient.getConnectionManager().shutdown();
		}
		return null;

	}

}
